package com.mall4j.cloud.search.config;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author FrozenWatermelon
 * @date 2020/9/24
 */

@Configuration
public class ElasticConfig {

    @Value("${elastic.address}")
    private String address;

    @Value("${elastic.port}")
    private Integer port;

    @Value("${elastic.password}")
    private String password;

    @Value("${elastic.username}")
    private String username;
    @Bean(name = "abc")
    public RestHighLevelClient restHighLevelClient() {
        Header[] headers = new BasicHeader[2];
        headers[0] = new BasicHeader("username",username);
        headers[1] = new BasicHeader("password",password);

/*
        RestClientBuilder builder = RestClient.builder(hosts.stream().toArray(HttpHost[]::new))
                .setDefaultHeaders(headers)
                .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
                    @Override
                    public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder) {
                        return builder.setConnectTimeout(5000 * 1000)
                                *//*
*/
/** 套接字超时（默认为30秒）//更改客户端的超时限制默认30秒现在改为100*1000分钟 *//*


                                .setSocketTimeout(6000 * 1000); //
                    }
                });
        RestHighLevelClient client =  new RestHighLevelClient(builder);*//*

       // return client;
*/
        return new RestHighLevelClient(
                RestClient.builder(
                        new HttpHost(address, port, "http")).setDefaultHeaders(headers));
    }
}

